package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.github.pagehelper.Page;
import com.rs.model.CommonResult;
import com.rs.model.ZhClasses;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhUserPersonnelService;
import com.rs.service.ZhUserStudentService;
import com.rs.service.impl.PublicUserMethodsImpl;
import com.rs.service.impl.ZhUserStudentServiceImpl;
import com.rs.util.DateChooserJButton;
import com.rs.util.SpringContextUtils;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


@SuppressWarnings("all")
public class SelectUser extends JFrame {

	JDesktopPane jDesktopPane;
	
	private JPanel contentPane;
	
	
	private ZhUserStudentService zhUserService  = SpringContextUtils.getBean(ZhUserStudentService.class);
	
	int conditionsPages; //条件查询的总页数
	int conditionsPageNum=1;//条件查询的页码
	
	int pageSum=0; //普通查询的列表页码
	private JTable table;
	private JPanel panel_deginTime;
	private JPanel panel_endTime;
	
	private PublicUserMethodsService pum  = SpringContextUtils.getBean(PublicUserMethodsService.class);
	
	private JTextField text_teacherName;
	private JTextField text_studentName;
	
	Map<String, Integer> mapca = new HashMap<>();
	
	JComboBox comboBox_class;
	DateChooserJButton dc= new DateChooserJButton();
	DateChooserJButton dc2 = new DateChooserJButton();
	
	SpinnerDateModel model = new SpinnerDateModel();
    JSpinner year = new JSpinner(model);
    
    private int poid;
    private int clId;
    
    ZhUser user;
    ZhUser user2;
    int timeSelect=1;//1为时间查询2为费时间查询的条件查询
    int  conditionsSelect = 1;//1为非条件查询 2为条件查询
		

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectUser frame = new SelectUser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectUser() {
			aa();
	}
	
	public SelectUser(int id,JDesktopPane jDesktopPane) {
		this.jDesktopPane=jDesktopPane;
		this.poid=id;
		aa();
	}
	public SelectUser(int id,int clId,JDesktopPane jDesktopPane) {
		this.jDesktopPane=jDesktopPane;
		this.poid=-1;
		this.clId=clId;
		aa();
	}
	
	public void aa(){
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 704, 662);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton button = new JButton("\u4E0A\u4E00\u9875");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(conditionsSelect ==1){
					upPage(poid);
				}else if(conditionsSelect ==1){
					conditionsUpPage();
				}	
			}
		});
		button.setBounds(349, 564, 93, 23);
		contentPane.add(button);
		
		JButton button_01 = new JButton("\u4E0B\u4E00\u9875");
		button_01.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(conditionsSelect ==1){
					nextPage(poid);
				}else if(conditionsSelect ==1){
					conditionsNextPage();
				}
			}
		});
		button_01.setBounds(468, 564, 93, 23);
		contentPane.add(button_01);
		
		
		JLabel lLabel_username = new JLabel("昵称");
		lLabel_username.setHorizontalAlignment(SwingConstants.CENTER);
		lLabel_username.setFont(new Font("等线", Font.PLAIN, 18));
		lLabel_username.setBounds(416, 164, 64, 34);
		contentPane.add(lLabel_username);
		
		JLabel label = new JLabel("性别");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("等线", Font.PLAIN, 18));
		label.setBounds(301, 164, 64, 34);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("姓名");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("等线", Font.PLAIN, 18));
		label_1.setBounds(178, 164, 64, 34);
		contentPane.add(label_1);
		
		JLabel lblId = new JLabel("id");
		lblId.setHorizontalAlignment(SwingConstants.CENTER);
		lblId.setFont(new Font("等线", Font.PLAIN, 18));
		lblId.setBounds(64, 164, 64, 34);
		contentPane.add(lblId);
		
		table = new JTable();
		table.setFont(new Font("等线", Font.PLAIN, 16));
		table.setRowHeight(34);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
				{null, null, null, null},
			},
			new String[] {
				"id", "\u59D3\u540D", "\u6027\u522B", "\u6635\u79F0"
			}
		));
		table.setBounds(40, 208, 472, 340);
		contentPane.add(table);
		
		JButton btn_selectStudent = new JButton("查看");
		btn_selectStudent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel tt =table.getModel();
				int selectedRow=table.getSelectedRow();
				int id =(Integer)tt.getValueAt(selectedRow, 0);
				selectStudnet(id);
			}
		});
		btn_selectStudent.setBounds(40, 558, 120, 34);
		contentPane.add(btn_selectStudent);
		
		JLabel label_2 = new JLabel("条件查询");
		label_2.setBounds(250, 10, 81, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("班级：");
		label_3.setBounds(64, 42, 54, 23);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("教师名：");
		label_4.setBounds(228, 42, 54, 23);
		contentPane.add(label_4);
		
		JLabel label_5 = new JLabel("学生姓名：");
		label_5.setBounds(399, 42, 81, 23);
		contentPane.add(label_5);
		
		text_teacherName = new JTextField();
		text_teacherName.setBounds(277, 42, 88, 23);
		contentPane.add(text_teacherName);
		text_teacherName.setColumns(10);
		
		text_studentName = new JTextField();
		text_studentName.setColumns(10);
		text_studentName.setBounds(460, 42, 88, 23);
		contentPane.add(text_studentName);
		
		comboBox_class = new JComboBox();
		comboBox_class.setFont(new Font("等线", Font.PLAIN, 14));
		comboBox_class.setBounds(104, 41, 101, 25);
		comboBox_class.addItem("无");
		contentPane.add(comboBox_class);
		
		List<ZhClasses> cl= pum.selectCaAll();
		for(ZhClasses cc:cl){
		comboBox_class.addItem(cc.getClName());
		mapca.put(cc.getClName(),cc.getClId());
		}
		
		JLabel lblNewLabel = new JLabel("入学时间：");
		lblNewLabel.setBounds(64, 98, 64, 23);
		contentPane.add(lblNewLabel);
	
		
		 panel_deginTime = new JPanel();
 		 panel_deginTime.setBounds(138, 98, 104, 27);
 		 contentPane.add(panel_deginTime);
 		   
 		 year.setValue(new Date());
         JSpinner.DateEditor editor = new JSpinner.DateEditor(year,"yyyy-MM-dd");
         year.setEditor(editor);
         year.setBounds(402, 298, 148, 27);
 		 panel_deginTime.add(dc);
 		 
 		 JLabel label_6 = new JLabel("--");
 		 label_6.setBounds(250, 102, 54, 15);
 		 contentPane.add(label_6);
 		 
 		 panel_endTime = new JPanel();
 		 panel_endTime.setBounds(277, 98, 104, 27);
 		 contentPane.add(panel_endTime);
 		 panel_endTime.add(dc2);
 		 
 		 JButton button_sel = new JButton("查询\r\n");
 		 button_sel.addActionListener(new ActionListener() {
 		 	public void actionPerformed(ActionEvent e) {
 		 		user =new ZhUser();
 		 		if(comboBox_class.getSelectedItem() !="无"){
 		 			user.setuCaid(mapca.get(comboBox_class.getSelectedItem()));	
 		 		}
		 		if(text_teacherName.getText() !=null && text_teacherName.getText().length()>0){
		 			user.setTeacherName(text_teacherName.getText());
		 		}
		 		if(text_studentName.getText() !=null && text_studentName.getText().length()>0){
		 			user.setStudentName(text_studentName.getText());
		 		}
		 		user.setuPoid(poid);
		 		/*Page<ZhUser> list = zhUserService.selectConditionStudents(user);
		 		SetTheValue(list);*/
		 		conditionsPageNum=1;
		 		conditionsNextPage();
 		 	}
 		 	
 		 });
 		 button_sel.setBounds(568, 42, 60, 23);
 		 contentPane.add(button_sel);
 		 
 		 JButton button_sel2 = new JButton("查询\r\n");
 		 button_sel2.addActionListener(new ActionListener() {
 		 	public void actionPerformed(ActionEvent e) {
 		 		user2 = new ZhUser();
 		 		user2.setDeginTime(dc.getDate());
 		 		user2.setEndTime(dc2.getDate());
 		 		user2.setuPoid(poid);
 		 		/*List<ZhUser> list = zhUserService.selectConditionStudents(user2);
		 		SetTheValue(list);*/
 		 		conditionsPageNum=1;
 		 		conditionsNextPage();
 		 		
 		 	}
 		 });
 		 button_sel2.setBounds(568, 98, 60, 23);
 		 contentPane.add(button_sel2);
		nextPage(poid);	
	}
	
	//条件查询的分页 上一页
	public void conditionsUpPage(){
		if(conditionsPageNum <= 2){
			alert("当前是第一页!!");
			return;
		}
		Page<ZhUser> page=null;
		if(timeSelect ==1){
			page = conditionsSelect();
		}else if (timeSelect ==2){
			page = conditionsSelectTime();
		}
		SetTheValue(page);
		conditionsPageNum--;
	}
	//条件查询的分页   下一页
	public void conditionsNextPage(){
		if(conditionsPageNum >=conditionsPages && conditionsPageNum!=1){
			alert("当前是最后一页！！");
			return;
		}
		Page<ZhUser> page=null;
		if(timeSelect ==1){
			page = conditionsSelect();
		}else if (timeSelect ==2){
			page = conditionsSelectTime();
		}
		SetTheValue(page);
		conditionsPageNum++;
	}
	
	private Page<ZhUser> conditionsSelect(){
		Page<ZhUser> list = zhUserService.selectConditionStudents(user);
		conditionsPages = list.getPages();
		return list;
	}
	
	private Page<ZhUser> conditionsSelectTime(){
		Page<ZhUser> list = zhUserService.selectConditionStudents(user2);
		conditionsPages = list.getPages();
		return list;
	}
	
	
	public void nextPage(int poid){
		pageSum++;	
		List<ZhUser> student = getStudent(poid,pageSum);
		if(student.size() == 0 && pageSum !=1){
			JOptionPane.showMessageDialog(this, "当前已是最后一页！");
			pageSum--;
			List<ZhUser> teachers = getStudent(poid,pageSum);
			SetTheValue(teachers);
			return;
		}
		SetTheValue(student);
	}
	
	
	
	public void upPage(int poid){
		if(pageSum<=1){
			JOptionPane.showMessageDialog(this, "当前是第一页！");
			List<ZhUser> student = getStudent(poid,pageSum);
			SetTheValue(student);
			return;
		}
		pageSum--;
		
		List<ZhUser> student = getStudent(poid,pageSum);
		SetTheValue(student);
	}
	
	public void SetTheValue(List<ZhUser> user){
		int sum =0;
		TableModel tb=table.getModel();
		listNull(tb,10,4);
		for(ZhUser tt :user){
			tb.setValueAt(tt.getuId(), sum, 0);
			tb.setValueAt(tt.getuName(), sum, 1);
			tb.setValueAt(tt.getuSex(), sum, 2);
			tb.setValueAt(tt.getuUsername(), sum, 3);
			sum++;
		}
		
	}
	
	public Object[][] listTurnAnArrayOf(List<ZhUser> list){
		 Object[][] obj =new  Object[10][4];
		 ZhUser user=new ZhUser();
		 for(int i=0;i<obj.length;i++){
			 user = list.get(i);	
			obj[i][0]=user.getuId();
			obj[i][1]=user.getuName();
			obj[i][2]=user.getuSex();
			obj[i][3]=user.getuUsername();
		 }
		 return obj;
	}
	public void listNull(TableModel tb,int row,int cou){
		 for(int i=0;i<row;i++){
			for(int j=0;j<cou;j++){
				tb.setValueAt(null, i, j);
			}
		 }
	}
	

	/**
	 * Create the frame.
	 */
	
	public void selectStudnet(int id){
		
		new UserInfo(1,id,jDesktopPane).setVisible(true);
	}
	
	
	private List<ZhUser> getStudent(int poid,int pageSum){
		ZhUser user=new ZhUser();
		user.setPageNum(pageSum);
		
		if(clId !=0){
			user.setuCaid(clId);
		}else{
			user.setuPoid(poid);
		}
		return zhUserService.selectConditionStudents(user);
	}
	private void alert(String aa){
		JOptionPane.showMessageDialog(this, aa);
	}
	//根据id得到用户详细
	private ZhUser getIdUser(Integer id){
		ZhUser uu = new ZhUser();
		uu.setuId(id);
		CommonResult<ZhUser> cc = zhUserService.selectStudent(uu);
		return cc.getData();
	}
}
